class Validator {
	constructor(a) {
		this.cache = [] //保存校验规则
	}
	strategies = {
		isNoEmpty(value, errorMsg) {
			return value === '' ?
				errorMsg : void 0
		},
		minLength(value, length, errorMsg) {
			return value.length < length ?
				errorMsg : void 0
		},
		isMoblie(value, errorMsg) {
			return !/^1\d{10}$/.test(value) ?
				errorMsg : void 0
		},
		isEmail(value, errorMsg) {
			return !/^\w+([+-.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(value) ?
				errorMsg : void 0
		},
		isIdCard(value,errorMsg){
			return !/^[1-9][0-9]{5}([1][9][0-9]{2}|[2][0][0|1][0-9])([0][1-9]|[1][0|1|2])([0][1-9]|[1|2][0-9]|[3][0|1])[0-9]{3}([0-9]|[X])$/.test(value) ?
				errorMsg : void 0
		},
		equal(val1,val2, errorMsg) {
			return val1 !== val2 ?
				errorMsg : void 0
		}
	}
	add(val, rules) {
		for (let rule of rules) {
			let strategyAry = rule.strategy.split(':');
			let errorMsg = rule.errorMsg;
			this.cache.push(() => {
				let strategy = strategyAry.shift();
				strategyAry.unshift(val);
				strategyAry.push(errorMsg);
				return this.strategies[strategy].apply(null, strategyAry)
			})
		}
	}
	start() {
		for (let validatorFunc of this.cache) {
			let errorMsg = validatorFunc() //开始校验，并取得校验后的返回信息
			if (errorMsg) { //r如果有确切返回值，说明校验没有通过
				return errorMsg
			}
		}
	}
}

export {
	Validator
}
